package vlms.servlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.IssuedMovie;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class HandleReturnMovie
 */
public class HandleReturnMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HandleReturnMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		try
		{
		HttpSession session = request.getSession(true);
		
		String movieId=request.getParameter("movieId");
		String issueId=request.getParameter("issuedId");
		
		System.out.println(movieId + issueId);
		IssuedMovie[] issuedMovieList=(IssuedMovie[])session.getAttribute("IssuedList");
		IssuedMovie tempObj=null;
		if(issuedMovieList!=null)
		{
			System.out.println(issuedMovieList.length);
		for(int i=0;i<issuedMovieList.length;i++)
		{
			IssuedMovie obj=issuedMovieList[i];
			if(obj.getIssueid()==Integer.parseInt(issueId))
			{
				tempObj=obj;
				tempObj.setIssuedstatus("RETURN");
				Calendar calendar=Calendar.getInstance();
				tempObj.setReturndate(calendar);
				
			}
			
		}
		
		
		String message=proxy.changeIssuedMovieStatus(tempObj);
		
		request.setAttribute("message", message);
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/returnMovie.jsp");
		dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			String message=e.getMessage();
			e.printStackTrace();
			request.setAttribute("message", message);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/returnMovie.jsp");
			dispatcher.forward(request, response);
		}
	}

}
